import 'terms.dart';

/// 方位复合判定
Terms directionTerms(
  int oppositeCount,
  int selfCount, {
  int? distanceSquared,
  List<ChessStep>? selfStepList,
  List<ChessStep>? oppositeStepList,
  ChessStep? nextStep,
  Board? board,
}) {
  printInfo('进入方向复合判定');
  // printInfo(
  //     'selfCount:$selfCount+selfStepList:$selfStepList+oppositeCount:$oppositeCount+oppositeStepList:$oppositeStepList');

  var desc = calculateDirection(nextStep!.toGtp());
  int s1 = 0;
  int s4 = 0;
  if (distanceSquared == 1) {
    s1 = oppositeCount;
  }
  s4 = selfCount;

  if (s1 == 1) {
    if (s4 == 1) {
      ChessStep step1 = oppositeStepList![0];
      ChessStep step4 = selfStepList![0];
      if (isOneOne(desc!)) {
        return Terms.oneOneEnter;
      } else if (isTwoTwo(desc) || isThreeThree(desc) || isStar(desc)) {
        // 进⼊⽅向复合判定
        // return _directionAbstractTerms(s1, s4,
        //     selfStepList: selfStepList,
        //     oppositeStepList: oppositeStepList,
        //     nextStep: nextStep);
        return Terms.none;
      } else if (step1.y == step4.y || step1.x == step4.x) {
        return Terms.top;
      } else {
        int x = nextStep.y;
        int y = 20 - nextStep.x;
        // 满⾜相对于S4的上⽅向
        if (step4.x == x) {
          if (y == step4.y - 1) {
            return Terms.press;
          }
          // 满⾜相对于S4的下⽅向
          else if (y == step4.y + 1) {
            return Terms.climb;
          }
        } else {
          // 满⾜相对于S4的左、右⽅向
          if (step4.y == y) {
            if (x == step1.x) {
              // 满⾜相对于S1的上⽅向:贴
              if (y == step1.y - 1) {
                return Terms.attach;
              }
              // 满⾜相对于S1的下⽅向:挡
              else if (y == step1.y + 1) {
                return Terms.resist;
              }
            }
          }
        }
      }
    } else if (s4 == 0) {
      // 特殊点,作为方向的分界线
      if (isTwoTwo(desc!)) {
        return Terms.twoTwoHold;
      } else if (isThreeThree(desc)) {
        return Terms.threeThreeHold;
      } else if (isStar(desc)) {
        return Terms.starDepend;
      } else {
        if (oppositeStepList!.length == 1) {
          var stepS1 = oppositeStepList[0];
          int x = nextStep.y;
          int y = 20 - nextStep.x;
          // 满⾜上⽅向
          if ((x == stepS1.x && y == stepS1.y - 1) ||
              y == stepS1.y && x == stepS1.x - 1) {
            if (desc.line == '四线') {
              return Terms.depend;
            } else if (isUnderFourQuadrant(desc)) {
              return Terms.hold;
            }
          }
          // 满⾜下⽅向
          else if ((x == stepS1.x && y == stepS1.y + 1) ||
              (y == stepS1.y && x == stepS1.x + 1)) {
            // S1&S3\S6\S7\S8\S9\S10 靠
            if (distanceSquared! > 3 &&
                distanceSquared < 11 &&
                distanceSquared != 7) {
              return Terms.depend;
            } else {
              return Terms.poke;
            }
          }
          // 满⾜左、右⽅向
          else {
            return Terms.depend;
          }
        }
      }
    }
    return Terms.none;
  }

  if (s4 == 1) {
    printInfo('进入特殊点判定');

    if (selfStepList!.isEmpty) {
      return Terms.none;
    }
    ChessStep step1 = selfStepList[0];
    ChessStep step2 = nextStep;

    bool isColorSame = step1.bw == step2.bw;

    int equal(String s1, String s2) {
      // step2是后手
      if (step1.toGtp() == s1 && step2.toGtp() == s2) {
        return 1;
      }
      // step2是先手
      else if (step1.toGtp() == s2 && step2.toGtp() == s1) {
        return -1;
      }
      return 0;
    }

    bool specialDot(List list, bool checkEqualToOne) {
      for (final pair in list) {
        final result = equal(pair[0], pair[1]);
        if (checkEqualToOne ? result == 1 : result == -1) {
          return true;
        }
      }
      return false;
    }

    printInfo('distanceSquared:$distanceSquared');
    // s6
    if (distanceSquared == 4) {
      if (isColorSame) {
        if (specialDot(s6Dots1, false)) {
          return Terms.smallEyeDgEnclosure;
        } else if (specialDot(s6Dots1, true)) {
          return Terms.smallEyeEnclosure;
        } else if (specialDot(s6Dots2, false)) {
          return Terms.smallEyeDgEnclosure;
        } else if (specialDot(s6Dots2, true)) {
          return Terms.smallEyeEnclosure;
        } else {
          Direction direction = isVertical(selfStepList, nextStep, distance: 2);
          // 满⾜上、下⽅向
          if (direction == Direction.up || direction == Direction.down) {
            return Terms.jump;
          } else {
            // 满⾜左、右⽅向
            return Terms.breakOne;
          }
        }
      } else {
        if (isOneOne(desc!) || isTwoTwo(desc)) {
          return Terms.dot;
        } else if (isThreeThree(desc)) {
          return Terms.threeThreeInvasion;
        } else {
          if (specialDot(s6Dots1, false)) {
            return Terms.smallEyeHighInvasion;
          } else if (specialDot(s6Dots1, true)) {
            return Terms.smallEyeInvasionCorner;
          } else if (specialDot(s6Dots2, false)) {
            return Terms.starHighInvasion;
          } else if (specialDot(s6Dots2, true)) {
            return Terms.starInvasionCorner;
          } else {
            Direction direction = isVertical(selfStepList, nextStep);
            //  满⾜上⽅向:镇
            if (direction == Direction.up) {
              return Terms.poke;
            }
            // 满⾜下⽅向:漏
            else if (direction == Direction.down) {
              return Terms.leak;
            }
            // 满⾜左、右⽅向：拦
            else if (direction == Direction.horizontal) {
              return Terms.block;
            }
          }
        }
      }
    }
    // s7
    else if (distanceSquared == 5) {
      if (specialDot(s7Dots1, false)) {
        return Terms.worryFreeCorner;
      } else if (specialDot(s7Dots1, true)) {
        return Terms.smallEyeEnclosure;
      } else if (specialDot(s7Dots2, false)) {
        return Terms.smallFlyEnclosure;
      } else if (specialDot(s7Dots2, true)) {
        return Terms.starEnclosure;
      } else {
        return Terms.smallFly;
      }
    } else if (distanceSquared == 9) {
      if (specialDot(s9Dots1, false)) {
        return Terms.bigJumpEnclosure;
      } else if (specialDot(s9Dots1, true)) {
        return Terms.smallEyeEnclosure;
      } else if (specialDot(s9Dots2, false)) {
        return Terms.starBigJumpEnclosure;
      } else if (specialDot(s9Dots2, true)) {
        return Terms.starBreakTwoEnclosure;
      }
      Direction direction = isVertical(selfStepList, nextStep, distance: 3);
      // 满⾜上、下⽅向
      if (direction == Direction.up || direction == Direction.down) {
        return Terms.bigJump;
      } else {
        // 满⾜左、右⽅向
        return Terms.breakTwo;
      }
    } else if (distanceSquared == 10) {
      if (specialDot(s10Dots1, false)) {
        return Terms.bigJumpEnclosure;
      } else if (specialDot(s10Dots1, true)) {
        return Terms.smallEyeEnclosure;
      } else if (specialDot(s10Dots2, false)) {
        return Terms.bigFlyEnclosure;
      } else if (specialDot(s10Dots2, true)) {
        return Terms.starEnclosure;
      } else {
        return Terms.bigFly;
      }
    }
    if (desc == null) {
      return Terms.none;
    }
  }
  return Terms.none;
}
